//
//  ProductDetailTvc.m
//  MJApp
//
//  Created by John on 15/3/30.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "ProductDetailTvc.h"
#import "ProductDetailInfoCell.h"
#import "ProductSizeCell.h"
#import "ProductColorCell.h"
#import "ProductBuyNumCell.h"
@class ProductDetailTvc;
@interface ProductDetailTvc()<UITableViewDataSource,UITableViewDelegate,ProductDetailInfoCellProtocol,ProductColorCellDelegate,ProductSizeCellDelegate>


//刷新表格cell的各个字典

{
    
    NSArray * _colorArr;
    NSArray * _sizeArr;
    NSDictionary * _selectInfoDic;
    NSInteger _buyNum;
    NSString * _colorId;
    NSString * _sizeId;
}



@property (weak, nonatomic) IBOutlet UIView *confirmBtn;

@property (weak, nonatomic) IBOutlet UITableView *tvc;

@property (weak, nonatomic) ProductDetailTvc * vc;
@property (nonatomic,strong)ProductBuyNumCell * numCell;




@end

@implementation ProductDetailTvc

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)awakeFromNib{

    [self UI];
}
- (void)setDetailDic:(NSDictionary *)detailDic{
    _detailDic = detailDic;
    _colorArr = detailDic[@"color_id"];
    _sizeArr = detailDic[@"size"];

}
- (void)UI{
    self.y = 0;//IOS_IPHONE_WINDOW_HIGH;
    self.x = 0;
    self.width = IOS_IPHONE_WINDOW_WIDE;
    self.height = IOS_IPHONE_WINDOW_HIGH;
    self.tvc.delegate = self;
    self.tvc.dataSource = self;
    self.backgroundColor = [UIColor colorWithRed:38/255.0 green:39/255.0 blue:54/255.0 alpha:0.5];
    self.tvc.backgroundColor = nil;
    self.tvc.bounces = NO;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 200, IOS_IPHONE_WINDOW_WIDE, IOS_IPHONE_WINDOW_HIGH-200)];
    view.backgroundColor = [UIColor whiteColor];
//
    
    [self addSubview:view];
    [self sendSubviewToBack:view];
    [self bringSubviewToFront:self.confirmBtn];
    [self bringSubviewToFront:self.tvc];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 4;
}
- (NSInteger)numberOfRowsInSection:(NSInteger)section{

    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.detailDic];
    switch (indexPath.row) {
        case 0:
        {
            ProductDetailInfoCell * cell = [ProductDetailInfoCell cellForTableview:tableView];
             cell.infoDic = dic;
            cell.delegate = self;
           
            return cell;
             
            break;
        }
        case 1:
        {
            ProductSizeCell * cell = [ProductSizeCell cellForTableview:tableView];
            cell.infoDic = dic;
            cell.sizeArr = _sizeArr;
            cell.delegate = self;
            return cell;
            
        }
        case 2:
        {
            ProductColorCell * cell = [ProductColorCell cellForTableview:tableView];
            cell.infoDic = dic;
            cell.colorArr = _colorArr;
            cell.delegate = self;
            return cell;
            
        }
        case 3:
        {
            ProductBuyNumCell * cell = [ProductBuyNumCell cellForTableview:tableView];
            cell.infoDic = dic;
            return cell;
            
        }
            
        default:
            break;
    }
   
    return nil;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    DSXTableViewCell * cell = (DSXTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellH;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IOS_IPHONE_WINDOW_WIDE, 50)];

    view.alpha = 0.1;
    return view;
}

-(void)ProductDetailInfoCellExitDetail:(ProductDetailInfoCell *)cell{

    [UIView animateWithDuration:0.2 animations:^{
        self.y = IOS_IPHONE_WINDOW_HIGH;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (IBAction)toConfirmOrder:(id)sender {
    
    if (_selectInfoDic == nil) {
        
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"请选择尺寸与大小"];
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:_selectInfoDic];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
    ProductBuyNumCell * cell = (ProductBuyNumCell * )[self.tvc cellForRowAtIndexPath:indexPath];
    
    //数量
    NSInteger num = cell.currNum;
    dic[@"num"] = @(num);
    //名字
    NSString *name = self.detailDic[@"name"];
    dic[@"name"] = name;
    //图片
    NSArray *imageS = self.detailDic[@"images"];
    if (imageS.count > 0) {
        
        NSString *imageStr = imageS[0][@"image"];
        dic[@"image"] = imageStr;
    }
    
    if ([self.delegate respondsToSelector:@selector(ProductDetailTvcToConfirmOrder:withDic:)]) {
        [self.delegate ProductDetailTvcToConfirmOrder:self withDic:dic];
    }
    
}
- (void)ProductColorCell:(ProductColorCell *)cell btn:(UIButton *)btn{
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    if (_sizeId!=nil) {
        param[@"size"] = _sizeId;
    }else{
         param[@"size"] = @"";
    }
    param[@"id"] = self.detailDic[@"id"];
    param[@"color_id"] = _colorArr[btn.tag][@"id"];

    [WLHTTPRequest getSizeOrColorParam:param usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        NSLog(@"%@",resultDictionary);
        if ([resultDictionary[@"status"] intValue]==105) {
            _sizeArr = resultDictionary[@"data"];
            NSIndexPath * indexPatn = [NSIndexPath indexPathForItem:1 inSection:0];
            [self.tvc reloadRowsAtIndexPaths:@[indexPatn] withRowAnimation:UITableViewRowAnimationNone];
        }else if ([resultDictionary[@"status"] intValue]==104){
            _colorArr = resultDictionary[@"data"];
            NSIndexPath * indexPatn = [NSIndexPath indexPathForItem:2 inSection:0];
            [self.tvc reloadRowsAtIndexPaths:@[indexPatn] withRowAnimation:UITableViewRowAnimationNone];
        }else if ([resultDictionary[@"status"] intValue] == 1) {
            
            _selectInfoDic = resultDictionary[@"data"];
        }
    } andFailureBlock:^(NSError *resultError) {
        
    }];
    
}

- (void)ProductSizeCell:(ProductSizeCell *)cell BtnClick:(UIButton *)btn{
    _sizeId = _sizeArr[btn.tag][@"size"];
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    if (_colorId!=nil) {
        param[@"color_id"] = _colorId;
    }else{
        param[@"color_id"] = @"";
    }
    param[@"id"] = self.detailDic[@"id"];
    param[@"size"] = _sizeArr[btn.tag][@"size"];
    [WLHTTPRequest getSizeOrColorParam:param usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        NSLog(@"%@",resultDictionary);
        if ([resultDictionary[@"status"] intValue]==105) {
            _sizeArr = resultDictionary[@"data"];
            NSIndexPath * indexPatn = [NSIndexPath indexPathForItem:1 inSection:0];
            [self.tvc reloadRowsAtIndexPaths:@[indexPatn] withRowAnimation:UITableViewRowAnimationNone];
        }else if ([resultDictionary[@"status"] intValue]==104){
            _colorArr = resultDictionary[@"data"];
            NSIndexPath * indexPatn = [NSIndexPath indexPathForItem:2 inSection:0];
            [self.tvc reloadRowsAtIndexPaths:@[indexPatn] withRowAnimation:UITableViewRowAnimationNone];
            
            NSString *nameStr = [btn titleForState:UIControlStateNormal];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"selectSize" object:nil userInfo:@{@"size":nameStr}];
        }
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}
//
//+ (void)show{
//    ProductDetailTvc * tvc = (ProductDetailTvc * )[[NSBundle mainBundle] loadNibNamed:@"ProductDetailTvc" owner:nil options:nil].lastObject;
//    [UIView animateWithDuration:0.2 animations:^{
//        tvc.y = 50;
//    }];
//    tvc.vc = tvc;
//}
//+ (void)dismiss{
//    
//}
@end
